var jstree = $.tree_create();
$(function() {
	jstree.init($('#tree'), {
		data: {
			type: 'json',
			async: true,
			url: 'tree-node/'
		},
		cookies: {},
		ui: {
			dots: false,
			animation: 250,
			context: false
		},
		rules: {
			deletable: ['category'],
			draggable: ['category'],
			dragrules: [
				//'category inside root',
				'category inside category',
				'category before category',
				'category after category'
			]
		},
		lang: {
			new_node: 'Untitled category',
			loading: ' '
		},
		callback: {
			onrgtclk: function(node, tree_obj, e) {
				e.preventDefault();
			},
			onmove: function(node, ref_node, type, tree_obj) {
				$.post('actions/move/', { id: $(node).attr('id'), ref_id: $(ref_node).attr('id'), type: type }, function(responseJson) {
				}, 'json');
			},
			beforechange: function(node, tree_obj) {
				//$('.richtext').each(function() { tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id')); });
			}, 
			onchange: function(node, tree_obj) {
				$.get('?id='+node.id, function() {
					if ($(node).attr('rel')=='root') {
						$('a.delete').addClass('disabled');
					} else {
						$('a.delete').removeClass('disabled');
					}
					$('a.save').addClass('disabled');
					//initTinyMce();
				});
			},
			ondelete: function(node, tree_obj) {
				$.post('actions/delete/', { id: $(node).attr('id') }, function(responseJson) {
				}, 'json');
			},
			onload: function(tree_obj) {
				$('#tree').find('.no-children').removeClass('open').removeClass('closed');
				if (!$('#tree a.clicked').length) {
					$('#tree a:first').click();
				}
			},
			onopen: function(node, tree_obj) {
				$(node).find('.no-children').removeClass('open').removeClass('closed').removeClass('no-children');
			}
		}
	});
	
	function jstree_create() {
		$.post('actions/create/', { parent_id: jstree.selected.attr('id') }, function(responseJson) {
			var already_opened = jstree.selected.find('li').length;
			jstree.selected.removeClass('leaf'); // open_branch fails when it has class "leaf" - I guess cos it assumes there's nothing beneath it to open.
			jstree.open_branch(jstree.selected, false, function() {
				if (already_opened) {
					// children loaded
					var new_li = jstree.create({
						"attributes": {
							"id": responseJson.id,
							"class": "inactive",
							"rel": "category",
							"parent_id": jstree.selected.attr('id')
						},
						"state": "closed",
						"data": {
							"title":"Untitled category",
							"icon":"/admin/backend/resources/images/icons/folder.png"
						}
					});
					jstree.select_branch(new_li);
					new_li.removeClass('open').removeClass('closed');
				} else {
					// never been opened
					jstree.select_branch(jstree.get_node(jstree.selected.find('li:last-child')));
				}
				//
			});
			$("ul.tabs").tabs('select', 0);
		}, 'json');
	}
	
	$('a.create').click(function(e) {
		e.preventDefault();
		jstree_create();
	});
	$('a.delete').click(function(e) {
		e.preventDefault();
		if (confirm('Are you sure you want to remove this category?')) {
			jstree.remove();
		}
	});
	
	
	/*
	 * Breadcrumb
	 *
	 */
	$('.breadcrumb a').livequery('click', function(e) {
		e.preventDefault();
		$.tree_reference('tree').select_branch($($(this).attr('href')));
	});
	
	
	
});